//
//  YbrMusicPlayUtil.m
//  wifibox
//
//  Created by os X 10.9 on 14-12-17.
//  Copyright (c) 2014年 yinbaner. All rights reserved.
//  音乐播放的工具类

#import "YbrMusicPlayUtil.h"

#import "YbrMusicItem.h"
//#import "YbrTempPlaylistDao.h"
//#import "YbrDownItem.h"
//#import "YbrTextClassInfoItem.h"
//#import "YbrTextClassMusicItem.h"
#import "YbrDLNAUtil.h"
#import "YbrDlnaPlayer.h"

@interface YbrMusicPlayUtil ()

@property (nonatomic)           int         playIndex;
@property (nonatomic, strong)   NSTimer     * timer;

@end



@implementation YbrMusicPlayUtil

static YbrPlayer        * player;
static YbrDlnaPlayer    * dlnaPlayer;



-(void)setDelegate:(id<YbrMusicPlayUtilDelegate>)delegate {
    _delegate = delegate;
    
    if(delegate == nil) {
        if(self.timer != nil) {
            [self.timer invalidate];
            self.timer = nil;
            return;
        }
    }
    //时间间隔
    NSTimeInterval timeInterval =1.0;
    //定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(timeOk) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    [self timeOk];
}

// 定时时间到了
-(void) timeOk {
    if ([self.delegate respondsToSelector:@selector(playCallBack:totalTime:)]) {
        [self.delegate playCallBack:[YbrMusicPlayUtil getPoisiton] totalTime:[YbrMusicPlayUtil getDuration]];
    }
}

+(void)initialize {
    if(player == nil) {
        //player = [[YbrPlayer alloc] init];
        
        player = [YbrPlayer getInstance];
        
        NSUserDefaults *userDefaults    =   [NSUserDefaults  standardUserDefaults];
        [userDefaults synchronize];
        int position                    =   [[userDefaults objectForKey:@"musicPos"] intValue];
        
//        [player addMusiclist:[[[YbrTempPlaylistDao alloc] init] getTemplist] position:position];
    }
    
    
    
    
    if(dlnaPlayer == nil) {
        
        dlnaPlayer = [[YbrDlnaPlayer alloc] init];
    }
}


// 播放所有的音频
// 播放哪一个
+(void) playAll:(NSArray *) musiclist position:(int) position{
    
    if ([YbrMusicPlayUtil isDlnaState]) {
        [player stopNoSendChange];
    }
    
    
    // 如果是dlna状态下
    if([YbrMusicPlayUtil isDlnaState]) {
        // 执行stop但是不要改变
        [player stopNoSendChange];
        [dlnaPlayer playAll:musiclist position:position];
        return;
    }
    
    // 判断推送的列表是否一样
    if(![YbrMusicPlayUtil isAlike:musiclist]) {
        // 插入数据库
//        YbrTempPlaylistDao *tempPlay = [[YbrTempPlaylistDao alloc] init];
//        [tempPlay setTemplistByArr:musiclist];        
        [player playAll:musiclist position:position];
    }else {
       [YbrMusicPlayUtil setQueuePosition:position];
    }
    
}


// 推送的烈烈表
+(void) PsuhPlayUrl:(NSString *) url {
    if([YbrMusicPlayUtil isDlnaState]) {
        [dlnaPlayer PsuhPlayUrl:url];
    }
}


// 当进行dlna播放的时候进行这个播放(可让其支持后台播放)
+(void) playOnDlna {
    [player playDlna];
}


// 是否一样两个列表
+(BOOL) isAlike:(NSArray *) musiclist {
    return [player isAlike:musiclist];
}


// 返回Arr : YbrMusicItem 正在播放的Item
+(YbrMusicItem *) getQueueItem {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer getQueueItem];
    }
    return [player getQueueItem];
}


// 是否在dlna状态
+(BOOL) isDlnaState {
    if([[YbrDLNAUtil getInstance] getSelectedDevice] == nil) {
        return NO;
    }
    return YES;
}


// 获取音频的课程名称
+(NSString *) getArtistName {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer getArtistName];
    }
    return [player getArtistName];
}


// 获取课程的创建者
+(NSString *) getSingerName {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer getSingerName];
    }
    return [player getSingerName];
}


// 获取音乐名称
+(NSString *) getMusicName {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer getMusicName];
    }
    return [player getMusicName];
}


// 获取位图
+(NSString *) getBitmap {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer getBitmap];
    }
    return [player getBitmap];
}


// 获取音频总时间
+(int) getDuration {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer getDuration];
    }
    return [player getDuration];
}


// 获取当前音频呢播放的位置
+(int) getPoisiton {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer getPoisiton];
    }
    return [player getPoisiton];
}


// 设置播放指定音频
+(void) setQueuePosition:(int) index {
    if([YbrMusicPlayUtil isDlnaState]) {
        [dlnaPlayer setQueuePosition:index];
        return;
    }
    [player setQueuePosition:index];
}


// 获取是否正在播放
+(BOOL) isPlaying {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer isPlaying];
    }
    return [player isPlaying];
}


// 获取当前播放的位置
+(int) getQueuePosition {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer getQueuePosition];
    }
    return [player getQueuePosition];
}


// 正在播放的音频的ID
+(int) getAudioId {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer getAudioId];
    }
    return [player getAudioId];
}


// 下一曲
+(void) Next {
    if([YbrMusicPlayUtil isDlnaState]) {
        [dlnaPlayer Next];
        return;
    }
    [player Next];
}


// 上一曲
+(void) Prev {
    if([YbrMusicPlayUtil isDlnaState]) {
        [dlnaPlayer Prev];
        return;
    }
    [player Prev];
}


// 播放或停止
+(void) PlayOrPause {
    if([YbrMusicPlayUtil isDlnaState]) {
        [dlnaPlayer PlayOrPause];
        return;
    }
    [player PlayOrPause];
}

// 播放
+(void) Play {
    if([YbrMusicPlayUtil isDlnaState]) {
        [dlnaPlayer Play];
        return;
    }
    [player Play];
}

// 暂停
+(void) Stop {
    if([YbrMusicPlayUtil isDlnaState]) {
        [dlnaPlayer stop];
        return;
    }
    
    NSLog(@"-----------------这里执行了停止哦-------------------------------------");
    
    [player stop];
}


// 停止dlna的获得信息
+(void) StopDlnaGetInfo {
    [dlnaPlayer stopGetInfo];
}


// 开始dlna获得信息
+(void) startDlnaGetInfo {
    if([YbrMusicPlayUtil isDlnaState]) {
        [dlnaPlayer startGetInfo];
    }
}


// 拖拽播放
+(void) seek:(NSTimeInterval) pos {
    if([YbrMusicPlayUtil isDlnaState]) {
        [dlnaPlayer seek:pos];
        return;
    }
    [player seek:pos];
}


// 获取音箱当前音量
+(float) getDlnaVolum {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer getVolum];
    }
    return 0.5;
}


// 设置音箱音量
+(void) setDlnaVolum:(float) value {
    if([YbrMusicPlayUtil isDlnaState]) {
        [dlnaPlayer setVolum:value];
    }
}


//  是否有音乐列表
+(BOOL) isHaveMusic {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer isHaveMusic];
    }
    return [player isHaveMusic];
}


// 获取是否是时间
+(BOOL) getIsTime {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer getIsTime];
    }
    return NO;
}


// 获取正在播放定时列表的ID
+(NSString *)getPlayTimeID {
    if([YbrMusicPlayUtil isDlnaState]) {
        return [dlnaPlayer getPlayTimeID];
    }
    return @"";
}


// 获取播放
+(NSArray *) getPlayArrAndRemLocal:(NSArray *) arr currPos:(int) currPos{
    NSMutableArray *tempArr = [NSMutableArray array];
    for (int i = 0; i < arr.count; i ++) {
        YbrMusicItem *item = [arr objectAtIndex:i];
        if(![item.file hasPrefix:@"ipod-library"]) {
            [tempArr addObject:item];
        }
    }
    
    int position = 0;
    int myCurrPos = currPos;
    if(myCurrPos > tempArr.count) {
        position = 0;
    }else {
        position = myCurrPos;
    }
    
    if(tempArr.count != arr.count) {
    }
    
    return [NSArray arrayWithObjects:tempArr ,[NSNumber numberWithInt:position], nil];
}


+(int) whichPositionAtMusicItem:(NSArray *)musicItemArr musicId:(int) musicId {
    if(musicItemArr == nil || musicItemArr.count == 0) {
        return -1;
    }
    for (int i = 0; i < musicItemArr.count; i ++) {
        if(musicId == ((YbrMusicItem *) musicItemArr[i]).musicId) {
            return i;
        }
    }
    return -1;
}

+(int) whichPositionAtMusicItemByName:(NSArray *)musicItemArr musicId:(NSString *) musicName {
    if(musicItemArr == nil || musicItemArr.count == 0) {
        return -1;
    }
    for (int i = 0; i < musicItemArr.count; i ++) {
        if([musicName isEqualToString:((YbrMusicItem *) musicItemArr[i]).musicName]) {
            return i;
        }
    }
    return -1;
}

+(int) whichPositionAtClassInfo:(NSArray *)classInfoArr musicId:(int) musicId {
    if(classInfoArr == nil || classInfoArr.count == 0) {
        return -1;
    }
    for (int i = 0; i < classInfoArr.count; i ++) {
        if(musicId == ((YbrClassInfoItem *) classInfoArr[i]).ID) {
            return i;
        }
    }
    return -1;
}

+(int) whichPositionAtClassInfo:(NSArray *)classInfoArr musicName:(NSString *)musicName {
    if(classInfoArr == nil || classInfoArr.count == 0) {
        return -1;
    }
    for (int i = 0; i < classInfoArr.count; i ++) {
        if([musicName isEqualToString:((YbrClassInfoItem *) classInfoArr[i]).name]) {
            return i;
        }
    }
    return -1;
}

// 这里相对特殊一点
+(NSIndexPath *) whichPositionAtTextClassInfo:(NSArray *)textClassInfoArr musicId:(int) musicId {
    if(textClassInfoArr == nil || textClassInfoArr.count == 0) {
        return nil;
    }
    for (int i = 0; i < textClassInfoArr.count; i ++) {
//        NSArray *arr = ((YbrTextClassInfoItem *)[textClassInfoArr objectAtIndex:i]).audiolist;
//        if(arr == nil || arr.count == 0) {
//            return nil;
//        }
//        for (int j = 0; j < arr.count; j++) {
//            if(musicId == ((YbrTextClassMusicItem *) arr[j])._id) {
//                return [NSIndexPath indexPathForRow:j inSection:i];
//            }
//        }
        
    }
    return nil;
}

+(int) whichPositionAtDownItem:(NSArray *)downItemArr musicId:(int) musicId {
    if(downItemArr == nil || downItemArr.count == 0) {
        return -1;
    }
    for (int i = 0; i < downItemArr.count; i ++) {
//        if(musicId == ((YbrDownItem *) downItemArr[i]).musicId) {
//            return i;
//        }
    }
    return -1;
}

+(int) whichPositionAtMusicHall:(NSArray *) cellArr musicId:(int) musicid {
    if(cellArr == nil || cellArr.count == 0) {
        return -1;
    }
    for (int i = 0; i < cellArr.count; i ++) {
        if([cellArr[i][@"id"] intValue] == musicid) {
            return i;
        }
    }
    return -1;
}


// 设置播放状态
+(void)setPlayState
{
    if([YbrMusicPlayUtil isDlnaState]) {
        [dlnaPlayer stop];
        return;
    }
    [player stop];
}



@end
